package com.braveman.im.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.CommentAddBean;
import com.braveman.im.bean.GetMomentsDeatils;
import com.braveman.im.bean.LikeAddBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.PhotoAdapterNew;
import com.braveman.im.ui.weiget.CommentAddPopup;
import com.braveman.im.ui.weiget.RecyclerItemClickListener;
import com.braveman.im.ui.weiget.SoftKeyboardStateHelper;
import com.braveman.im.utils.UtilsStyle;
import com.bumptech.glide.Glide;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.iflytek.cloud.thirdparty.V;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.vondear.rxtools.RxBarTool;
import com.vondear.rxtools.RxKeyboardTool;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import me.iwf.photopicker.PhotoPreview;
import retrofit2.Call;
import retrofit2.Response;

public class MineMomentDetailsActivity extends BaseActivity {
    private PhotoAdapterNew photoAdapter;
    private ArrayList<String> photos = new ArrayList<>();
    private String id;
    private RecyclerView mRecyclerView;
    private QMUIRadiusImageView mQmuiAvater;
    private TextView mTvName;
    private TextView mTvDes;
    private TextView mTvTimeBefore;
    private TextView mTvDelete;
    private ImageView mIvCircleIconComment;
    private ImageView mIvCircleDetailIconZan;
    private ImageView mIvCircleIconShare;
    private EditText mEtCommentAddEdit;
    private LinearLayout mLlCommentAddEdit;
    private ScrollView mActivityMainLayout;


    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("详情").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(MineMomentDetailsActivity.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_mine_moment_details;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        id = callIntent.getStringExtra("id");
    }

    @Override
    protected void initChildView() {
        mQmuiAvater = (QMUIRadiusImageView) findViewById(R.id.qmui_avater);
        mEtCommentAddEdit = (EditText) findViewById(R.id.et_comment_add_edit);
        mLlCommentAddEdit = (LinearLayout) findViewById(R.id.ll_comment_add_edit);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mTvDes = (TextView) findViewById(R.id.tv_des);
        mTvTimeBefore = (TextView) findViewById(R.id.tv_time_before);
        mTvDelete = (TextView) findViewById(R.id.tv_delete);
        mIvCircleIconComment = (ImageView) findViewById(R.id.iv_circle_icon_comment);
        mIvCircleDetailIconZan = (ImageView) findViewById(R.id.iv_circle_detail_icon_zan);
        mIvCircleIconShare = (ImageView) findViewById(R.id.iv_circle_icon_share);
        photoAdapter = new PhotoAdapterNew(MineMomentDetailsActivity.this, photos);
        mRecyclerView = findViewById(R.id.lv_Activity_list_member);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        mRecyclerView.setAdapter(photoAdapter);
        mActivityMainLayout = (ScrollView) findViewById(R.id.activity_main_layout);


    }

    @Override
    protected void initChildData() {
        if (TextUtils.isEmpty(id)) {
            UIUtils.showToast("诚友圈id为空");
            return;
        }
        ApiManager.getApi().getMineMomentDetails(id).enqueue(new NetCallBack<GetMomentsDeatils>() {
            @Override
            protected void onSuccess(Call<GetMomentsDeatils> call, Response<GetMomentsDeatils> response) {
                if (response != null) {
                    GetMomentsDeatils getMomentsDeatils = response.body();
                    if (getMomentsDeatils != null) {
                        Glide.with(MineMomentDetailsActivity.this).load(getMomentsDeatils.publisherAvatar).into(mQmuiAvater);
                        mTvName.setText(getMomentsDeatils.publisherName);
                        mTvDes.setText(getMomentsDeatils.content);
                        if (getMomentsDeatils.photoList != null) {
                            photos.addAll(getMomentsDeatils.photoList);
                            if (photoAdapter != null) {
                                photoAdapter.notifyDataSetChanged();
                            }
                            mIvCircleDetailIconZan.setImageResource(getMomentsDeatils.ifLike ? R.mipmap.circle_detail_icon_zan_pre : R.mipmap.circle_detail_icon_zan);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(MineMomentDetailsActivity.this,
                new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int positiona) {
                        PhotoPreview.builder()
                                .setPhotos(photos)
                                .setCurrentItem(positiona)
                                .start(MineMomentDetailsActivity.this);
                    }
                }));
        mTvDelete.setOnClickListener(this);
        mIvCircleIconComment.setOnClickListener(this);
        mIvCircleDetailIconZan.setOnClickListener(this);
        mIvCircleIconShare.setOnClickListener(this);
        findViewById(R.id.tv_activity_send_message).setOnClickListener(this);
        SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout));
        softKeyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                //键盘打开
            }

            @Override
            public void onSoftKeyboardClosed() {
                //键盘关闭
                mLlCommentAddEdit.setVisibility(View.GONE);
            }
        });

        mActivityMainLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                RxKeyboardTool.hideSoftInput(MineMomentDetailsActivity.this);
                mLlCommentAddEdit.setVisibility(View.GONE);
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_delete://删除
                doNetWork(0);
                break;
            case R.id.iv_circle_icon_comment://评论
                mLlCommentAddEdit.setVisibility(View.VISIBLE);
                RxKeyboardTool.showSoftInput(this, mEtCommentAddEdit);
                break;
            case R.id.iv_circle_detail_icon_zan://点赞
                doNetWork(1);
                break;
            case R.id.iv_circle_icon_share://分享
                break;
            case R.id.tv_activity_send_message://发送
                RxKeyboardTool.hideSoftInput(this);
                doNetWork(2);
                break;
        }

    }

    private void doNetWork(int type) {
        switch (type) {
            case 0:
                showLoadingDialog();
                ApiManager.getApi().deleteMoment(id).enqueue(new NetCallBack<GetMomentsDeatils>() {
                    @Override
                    protected void onSuccess(Call<GetMomentsDeatils> call, Response<GetMomentsDeatils> response) {
                        if (response != null) {
                            UIUtils.showToast("删除成功");
                            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_UPDATA_DELETE_MOMENT));
                            ActivityUtils.finishActivity(MineMomentDetailsActivity.this);

                        }
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case 1:
                LikeAddBean likeAddBean = new LikeAddBean();
                likeAddBean.momentId = id;
                showLoadingDialog();
                ApiManager.getApi().likeAdd(likeAddBean).enqueue(new NetCallBack<LikeAddBean>() {
                    @Override
                    protected void onSuccess(Call<LikeAddBean> call, Response<LikeAddBean> response) {
                        if (response.body() != null) {
                            LikeAddBean likeAddBean1 = response.body();
                            if (likeAddBean1 != null) {
                                UIUtils.showToast(likeAddBean1.ifLike ? "已点赞" : "已取消点赞");
                            }
                            mIvCircleDetailIconZan.setImageResource(likeAddBean1.ifLike ? R.mipmap.circle_detail_icon_zan_pre : R.mipmap.circle_detail_icon_zan);
                        }

                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case 2:
                CommentAddBean commentAddBean = new CommentAddBean();
                commentAddBean.momentId = id;
                String content = mEtCommentAddEdit.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    UIUtils.showToast("评论不能为空");
                    return;
                }
                commentAddBean.content = content;
                showLoadingDialog();
                ApiManager.getApi().commentAdd(commentAddBean).enqueue(new NetCallBack<CommentAddBean>() {
                    @Override
                    protected void onSuccess(Call<CommentAddBean> call, Response<CommentAddBean> response) {
                        if (response != null) {
                            UIUtils.showToast("评论已发送");
                        }
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;

        }


    }
}
